<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends CI_Model{
	// 登录验证
	public function loginup($admin_name,$admin_pwd)
	{
		$arr=$this ->db ->where(['admin_name'=>$admin_name,'admin_pwd'=>$admin_pwd])
		->get('admin')->row_array();
		return $arr;
	}
	// 注册验证唯一性
	public function register($admin_name)
	{
		$arr=$this ->db ->where(['admin_name'=>$admin_name])
		->get('admin')->row_array();
		return $arr;
	}
	// 添加管理员
	public function adminadd($data)
	{
		$arr=$this ->db ->insert('admin',$data);
		return $arr;
	}
	// 查询一条数据
	public function getone($id)
	{
		$arr=$this ->db ->where(['admin_id'=>$id])
		->get('admin')->row_array();
		return $arr;
	}
	// 修改管理员
	public function updateone($data)
	{
		// return $data;
		$res=$this ->db ->where('admin_id',$data['admin_id'])
		->update('admin',$data);
		return $res;
	}
	// 删除管理员
	public function admindel($admin_id)
	{
		// return $data;
		$res=$this ->db ->delete('admin',['admin_id'=>$admin_id]);
		return $res;
	}
	// admin_role中间表和角色表联查  查旧角色
	public function admin_role($admin_id)
	{
		$data=$this->db->select('r.role_id,r.role_name')
		->from('admin_role as a_r')
		->join('role as r','a_r.role_id=r.role_id')
		->where(['admin_id'=>$admin_id])
		->get()
		->result_array();
		return $data;
	}
	// 查询该角色的所有节点 5表联查
	public function access_route($admin_id)
	{
		$data=$this->db->select('ac.access_route')
		->from('admin as a')
		->join('admin_role as a_r','a.admin_id=a_r.admin_id')
		->join('role as r','r.role_id=a_r.role_id')
		->join('role_access as r_a','r_a.role_id=r.role_id')
		->join('access as ac','ac.access_id=r_a.access_id')
		->where(['a.admin_id'=>$admin_id])
		->get()
		->result_array();
		// 降维

		foreach ($data as $k => $v) {
			
			//用implode方法,将一维数组转换为用逗号连接的字符串
			$arr[]=$v['access_route'];
			$str=implode(',',$arr);
			//去掉重复的字符串,也就是重复的一维数组,重新生成一个数组
			$arr=array_unique($arr);
		}
		return $arr;
		

	}

}








 ?>